<?php defined('BASEPATH') OR exit('No direct script access allowed');


class Progress_bar
{
    var $_CI;
    var $_FOLDER_PATH;
    var $fopen;
    var $log;

    public function __construct()
    {
        $this->_CI = & get_instance();
        $this->_CI->load->helper('file');
        $this->_FOLDER_PATH = PROCESS_FOLDER;
    }

    public function get_file_name($key)
    {
        return $this->_FOLDER_PATH . '/' . $key . '.text';
    }

    public function log($data, $type = 'status', $write = true)
    {
        $this->log[date('H:i:s') . ' - ' . substr((string)microtime(), 2, 6)][$type] = $data;
        if ($write)
            $this->write();
    }

    public function open_file($key)
    {
        if (!is_dir($this->_FOLDER_PATH))
            @mkdir($this->_FOLDER_PATH);
        $this->fopen = @fopen($this->get_file_name($key), 'wb');
        return $this->fopen;
    }

    public function close_file()
    {
        @fclose($this->fopen);
    }

    public function delete_cache($key)
    {
        @unlink($this->get_file_name($key));
    }

    public function write()
    {
        rewind($this->fopen);
        fwrite($this->fopen, json_encode($this->log));
    }

    public function read($key)
    {
        return @file_get_contents($this->get_file_name($key));
    }
}